package com.weme.common.util

import android.content.Context
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.PutObjectRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File


class AwsS3Uploader private constructor() {

    private val s3Client: AmazonS3 by lazy {

        val credentials = BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)
       // val region = Region.getRegion(Regions.US_EAST_1)
        AmazonS3Client(credentials)
    }

    companion object {
        private const val AWS_ACCESS_KEY = "AKIAS2R45BICSG77DRV5"
        private const val AWS_SECRET_KEY = "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"
        private const val BUCKET_NAME = "backups3d"
        private const val IMAGE_PATH = "male/user_image/"
        private const val VIDEO_PATH = "male/user_video/"
        private const val SERVICE_IMAGE = "male/service_image/"
        private const val IMAGE_HEAD = "https://cloud.hva-mi.com/"

        @Volatile
        private var instance: AwsS3Uploader? = null

        fun getInstance(context: Context): AwsS3Uploader {
            return instance ?: synchronized(this) {
                instance ?: AwsS3Uploader().also { instance = it }
            }
        }
    }

    interface UploadListener {
        fun onProgressChanged(progress: Int)
        fun onUploadComplete(url: String)
        fun onUploadFailed()
    }

    private var uploadListener: UploadListener? = null

    fun setUploadListener(listener: UploadListener) {
        uploadListener = listener
    }

    fun clearUploadListener() {
        uploadListener = null
    }

    fun uploadImage(localFile: File) {
        uploadFile(localFile, IMAGE_PATH)
    }

    fun uploadVideo(localFile: File) {
        uploadFile(localFile, VIDEO_PATH,true)
    }
    fun uploadServiceImage(localFile: File) {
        uploadFile(localFile, SERVICE_IMAGE)
    }



    private fun uploadFile(localFile: File, remotePath: String,isVideo:Boolean ? = false) {
        GlobalScope.launch(Dispatchers.IO) {
            try {
                val remoteKey = "${remotePath}${System.currentTimeMillis()}" + if(isVideo == true) ".mp4" else ".png" // 修改后缀和文件名
                val request = PutObjectRequest(BUCKET_NAME, remoteKey, localFile)
                    .withProgressListener { progressEvent ->
                        val totalBytes = localFile.length()
                        val percent = (progressEvent.bytesTransferred.toFloat() / totalBytes.toFloat() * 100).toInt()
                        uploadListener?.onProgressChanged(percent)
                    }
                s3Client.putObject(request)
                uploadListener?.onUploadComplete("$IMAGE_HEAD$remoteKey")
            } catch (e: Exception) {
                e.printStackTrace()
                uploadListener?.onUploadFailed()
            }
        }
    }
}
